package dyyx;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

// import com.netflix.appinfo.InstanceInfo;
// import com.netflix.discovery.DiscoveryClient;

@RestController
//@Scope("prototype")
public class HelloController {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	
	@Autowired
	private RestTemplate restTemplate;
    // private RestTemplate restTemplate = new RestTemplate();
	
	@Autowired
	private RestTemplate eurekaRestTemplate;
	
	
	private static final String VERSION = "spring-cloud-consumer-2019-05-18 13:20";

	@GetMapping(value = "/status")
    public Map status() throws Exception {
		Map map = new HashMap();
		map.put("version",VERSION);
        return map;
    }

    @GetMapping(value = "/hello")
    public String hello() throws Exception {
        // return "hello spring cloud consumer,"+new Date();
    	
    	return restTemplate.getForObject("http://127.0.0.1:9031/hello", String.class);
    }
    
    @GetMapping(value = "/serverInfo")
    public Map<String,Object> serverInfo() throws Exception {
    	
    	Map<String,Object> map = eurekaRestTemplate.getForObject("http://provider/serverInfo", Map.class);
    	
    	if(map==null){
    		map = new HashMap<String,Object>();
    	}
    	map.put("type", "consumer");
    	
    	return map;
    }
    
    
    @GetMapping(value = "/discoveryInfo")
    public Map<String,Object> discoveryInfo() throws Exception {
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<ServiceInstance> list = discoveryClient.getInstances("provider");
    	List<String> services = discoveryClient.getServices();
    	map.put("serviceInstances",list);
    	map.put("services",services);

    	return map;
    }
    
    
    
}